package com.naiterui.ehp.bs.consult.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 上传工具类
 *
 * @author yangfuya
 * @version 1.0
 * @date 2021-02-26 11:31
 **/
public class UploadUtil {

    /**
     * 上传文件
     *
     * @param basePath 基础路径
     * @param path     地址
     * @param file     文件
     * @return 文件地址
     * @throws IOException
     */
    public static String uploadFile(String basePath, String path, MultipartFile file) throws IOException {
        String fileName = UUID.randomUUID() + "." + FilenameUtils.getExtension(file.getOriginalFilename());
        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(basePath + path + fileName));
        return path + fileName;
    }
}
